home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_urllib2.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  28KB  |  906 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. import os
  7. import socket
  8. import StringIO
  9. import urllib2
  10. from urllib2 import Request, OpenerDirector
  11.  
  12. class TrivialTests(unittest.TestCase):
  13.     
  14.     def test_trivial(self):
  15.         self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')
  16.         fname = os.path.abspath(urllib2.__file__).replace('\\', '/')
  17.         if fname[1:2] == ':':
  18.             fname = fname[2:]
  19.         
  20.         if os.name == 'mac':
  21.             fname = '/' + fname.replace(':', '/')
  22.         elif os.name == 'riscos':
  23.             import string as string
  24.             fname = os.expand(fname)
  25.             fname = fname.translate(string.maketrans('/.', './'))
  26.         
  27.         file_url = 'file://%s' % fname
  28.         f = urllib2.urlopen(file_url)
  29.         buf = f.read()
  30.         f.close()
  31.  
  32.     
  33.     def test_parse_http_list(self):
  34.         tests = [
  35.             ('a,b,c', [
  36.                 'a',
  37.                 'b',
  38.                 'c']),
  39.             ('path"o,l"og"i"cal, example', [
  40.                 'path"o,l"og"i"cal',
  41.                 'example']),
  42.             ('a, b, "c", "d", "e,f", g, h', [
  43.                 'a',
  44.                 'b',
  45.                 '"c"',
  46.                 '"d"',
  47.                 '"e,f"',
  48.                 'g',
  49.                 'h']),
  50.             ('a="b\\"c", d="e\\,f", g="h\\\\i"', [
  51.                 'a="b"c"',
  52.                 'd="e,f"',
  53.                 'g="h\\i"'])]
  54.         for string, list in tests:
  55.             self.assertEquals(urllib2.parse_http_list(string), list)
  56.         
  57.  
  58.  
  59.  
  60. class MockOpener:
  61.     addheaders = []
  62.     
  63.     def open(self, req, data = None):
  64.         self.req = req
  65.         self.data = data
  66.  
  67.     
  68.     def error(self, proto, *args):
  69.         self.proto = proto
  70.         self.args = args
  71.  
  72.  
  73.  
  74. class MockFile:
  75.     
  76.     def read(self, count = None):
  77.         pass
  78.  
  79.     
  80.     def readline(self, count = None):
  81.         pass
  82.  
  83.     
  84.     def close(self):
  85.         pass
  86.  
  87.  
  88.  
  89. class MockHeaders(dict):
  90.     
  91.     def getheaders(self, name):
  92.         return self.values()
  93.  
  94.  
  95.  
  96. class MockResponse(StringIO.StringIO):
  97.     
  98.     def __init__(self, code, msg, headers, data, url = None):
  99.         StringIO.StringIO.__init__(self, data)
  100.         (self.code, self.msg, self.headers, self.url) = (code, msg, headers, url)
  101.  
  102.     
  103.     def info(self):
  104.         return self.headers
  105.  
  106.     
  107.     def geturl(self):
  108.         return self.url
  109.  
  110.  
  111.  
  112. class MockCookieJar:
  113.     
  114.     def add_cookie_header(self, request):
  115.         self.ach_req = request
  116.  
  117.     
  118.     def extract_cookies(self, response, request):
  119.         self.ec_req = request
  120.         self.ec_r = response
  121.  
  122.  
  123.  
  124. class FakeMethod:
  125.     
  126.     def __init__(self, meth_name, action, handle):
  127.         self.meth_name = meth_name
  128.         self.handle = handle
  129.         self.action = action
  130.  
  131.     
  132.     def __call__(self, *args):
  133.         return self.handle(self.meth_name, self.action, *args)
  134.  
  135.  
  136.  
  137. class MockHandler:
  138.     
  139.     def __init__(self, methods):
  140.         self._define_methods(methods)
  141.  
  142.     
  143.     def _define_methods(self, methods):
  144.         for spec in methods:
  145.             if len(spec) == 2:
  146.                 (name, action) = spec
  147.             else:
  148.                 name = spec
  149.                 action = None
  150.             meth = FakeMethod(name, action, self.handle)
  151.             setattr(self.__class__, name, meth)
  152.         
  153.  
  154.     
  155.     def handle(self, fn_name, action, *args, **kwds):
  156.         self.parent.calls.append((self, fn_name, args, kwds))
  157.         if action is None:
  158.             return None
  159.         elif action == 'return self':
  160.             return self
  161.         elif action == 'return response':
  162.             res = MockResponse(200, 'OK', { }, '')
  163.             return res
  164.         elif action == 'return request':
  165.             return Request('http://blah/')
  166.         elif action.startswith('error'):
  167.             code = action[action.rfind(' ') + 1:]
  168.             
  169.             try:
  170.                 code = int(code)
  171.             except ValueError:
  172.                 pass
  173.  
  174.             res = MockResponse(200, 'OK', { }, '')
  175.             return self.parent.error('http', args[0], res, code, '', { })
  176.         elif action == 'raise':
  177.             raise urllib2.URLError('blah')
  178.         
  179.         if not False:
  180.             raise AssertionError
  181.  
  182.     
  183.     def close(self):
  184.         pass
  185.  
  186.     
  187.     def add_parent(self, parent):
  188.         self.parent = parent
  189.         self.parent.calls = []
  190.  
  191.     
  192.     def __lt__(self, other):
  193.         if not hasattr(other, 'handler_order'):
  194.             return True
  195.         
  196.         return self.handler_order < other.handler_order
  197.  
  198.  
  199.  
  200. def add_ordered_mock_handlers(opener, meth_spec):
  201.     '''Create MockHandlers and add them to an OpenerDirector.
  202.  
  203.     meth_spec: list of lists of tuples and strings defining methods to define
  204.     on handlers.  eg:
  205.  
  206.     [["http_error", "ftp_open"], ["http_open"]]
  207.  
  208.     defines methods .http_error() and .ftp_open() on one handler, and
  209.     .http_open() on another.  These methods just record their arguments and
  210.     return None.  Using a tuple instead of a string causes the method to
  211.     perform some action (see MockHandler.handle()), eg:
  212.  
  213.     [["http_error"], [("http_open", "return request")]]
  214.  
  215.     defines .http_error() on one handler (which simply returns None), and
  216.     .http_open() on another handler, which returns a Request object.
  217.  
  218.     '''
  219.     handlers = []
  220.     count = 0
  221.     for meths in meth_spec:
  222.         
  223.         class MockHandlerSubclass(MockHandler):
  224.             pass
  225.  
  226.         h = MockHandlerSubclass(meths)
  227.         h.handler_order = count
  228.         h.add_parent(opener)
  229.         count = count + 1
  230.         handlers.append(h)
  231.         opener.add_handler(h)
  232.     
  233.     return handlers
  234.  
  235.  
  236. class OpenerDirectorTests(unittest.TestCase):
  237.     
  238.     def test_handled(self):
  239.         o = OpenerDirector()
  240.         meth_spec = [
  241.             [
  242.                 'http_open',
  243.                 'ftp_open',
  244.                 'http_error_302'],
  245.             [
  246.                 'ftp_open'],
  247.             [
  248.                 ('http_open', 'return self')],
  249.             [
  250.                 ('http_open', 'return self')]]
  251.         handlers = add_ordered_mock_handlers(o, meth_spec)
  252.         req = Request('http://example.com/')
  253.         r = o.open(req)
  254.         self.assertEqual(r, handlers[2])
  255.         calls = [
  256.             (handlers[0], 'http_open'),
  257.             (handlers[2], 'http_open')]
  258.         for expected, got in zip(calls, o.calls):
  259.             (handler, name, args, kwds) = got
  260.             self.assertEqual((handler, name), expected)
  261.             self.assertEqual(args, (req,))
  262.         
  263.  
  264.     
  265.     def test_handler_order(self):
  266.         o = OpenerDirector()
  267.         handlers = []
  268.         for meths, handler_order in [
  269.             ([
  270.                 ('http_open', 'return self')], 500),
  271.             ([
  272.                 'http_open'], 0)]:
  273.             
  274.             class MockHandlerSubclass(MockHandler):
  275.                 pass
  276.  
  277.             h = MockHandlerSubclass(meths)
  278.             h.handler_order = handler_order
  279.             handlers.append(h)
  280.             o.add_handler(h)
  281.         
  282.         r = o.open('http://example.com/')
  283.         self.assertEqual(o.calls[0][0], handlers[1])
  284.         self.assertEqual(o.calls[1][0], handlers[0])
  285.  
  286.     
  287.     def test_raise(self):
  288.         o = OpenerDirector()
  289.         meth_spec = [
  290.             [
  291.                 ('http_open', 'raise')],
  292.             [
  293.                 ('http_open', 'return self')]]
  294.         handlers = add_ordered_mock_handlers(o, meth_spec)
  295.         req = Request('http://example.com/')
  296.         self.assertRaises(urllib2.URLError, o.open, req)
  297.         self.assertEqual(o.calls, [
  298.             (handlers[0], 'http_open', (req,), { })])
  299.  
  300.     
  301.     def test_http_error(self):
  302.         o = OpenerDirector()
  303.         meth_spec = [
  304.             [
  305.                 ('http_open', 'error 302')],
  306.             [
  307.                 ('http_error_400', 'raise'),
  308.                 'http_open'],
  309.             [
  310.                 ('http_error_302', 'return response'),
  311.                 'http_error_303',
  312.                 'http_error'],
  313.             [
  314.                 'http_error_302']]
  315.         handlers = add_ordered_mock_handlers(o, meth_spec)
  316.         
  317.         class Unknown:
  318.             
  319.             def __eq__(self, other):
  320.                 return True
  321.  
  322.  
  323.         req = Request('http://example.com/')
  324.         r = o.open(req)
  325.         if not len(o.calls) == 2:
  326.             raise AssertionError
  327.         calls = [
  328.             (handlers[0], 'http_open', (req,)),
  329.             (handlers[2], 'http_error_302', (req, Unknown(), 302, '', { }))]
  330.         for expected, got in zip(calls, o.calls):
  331.             (handler, method_name, args) = expected
  332.             self.assertEqual((handler, method_name), got[:2])
  333.             self.assertEqual(args, got[2])
  334.         
  335.  
  336.     
  337.     def test_processors(self):
  338.         o = OpenerDirector()
  339.         meth_spec = [
  340.             [
  341.                 ('http_request', 'return request'),
  342.                 ('http_response', 'return response')],
  343.             [
  344.                 ('http_request', 'return request'),
  345.                 ('http_response', 'return response')]]
  346.         handlers = add_ordered_mock_handlers(o, meth_spec)
  347.         req = Request('http://example.com/')
  348.         r = o.open(req)
  349.         calls = [
  350.             (handlers[0], 'http_request'),
  351.             (handlers[1], 'http_request'),
  352.             (handlers[0], 'http_response'),
  353.             (handlers[1], 'http_response')]
  354.         for handler, name, args, kwds in enumerate(o.calls):
  355.             if i < 2:
  356.                 self.assertEqual((handler, name), calls[i])
  357.                 self.assertEqual(len(args), 1)
  358.                 self.assert_(isinstance(args[0], Request))
  359.                 continue
  360.             self.assertEqual((handler, name), calls[i])
  361.             self.assertEqual(len(args), 2)
  362.             self.assert_(isinstance(args[0], Request))
  363.             if not args[1] is None:
  364.                 pass
  365.             self.assert_(isinstance(args[1], MockResponse))
  366.         
  367.  
  368.  
  369.  
  370. def sanepathname2url(path):
  371.     import urllib as urllib
  372.     urlpath = urllib.pathname2url(path)
  373.     if os.name == 'nt' and urlpath.startswith('///'):
  374.         urlpath = urlpath[2:]
  375.     
  376.     return urlpath
  377.  
  378.  
  379. class HandlerTests(unittest.TestCase):
  380.     
  381.     def test_ftp(self):
  382.         
  383.         class MockFTPWrapper:
  384.             
  385.             def __init__(self, data):
  386.                 self.data = data
  387.  
  388.             
  389.             def retrfile(self, filename, filetype):
  390.                 self.filename = filename
  391.                 self.filetype = filetype
  392.                 return (StringIO.StringIO(self.data), len(self.data))
  393.  
  394.  
  395.         
  396.         class NullFTPHandler(urllib2.FTPHandler):
  397.             
  398.             def __init__(self, data):
  399.                 self.data = data
  400.  
  401.             
  402.             def connect_ftp(self, user, passwd, host, port, dirs):
  403.                 self.user = user
  404.                 self.passwd = passwd
  405.                 self.host = host
  406.                 self.port = port
  407.                 self.dirs = dirs
  408.                 self.ftpwrapper = MockFTPWrapper(self.data)
  409.                 return self.ftpwrapper
  410.  
  411.  
  412.         import ftplib as ftplib
  413.         import socket as socket
  414.         data = 'rheum rhaponicum'
  415.         h = NullFTPHandler(data)
  416.         o = h.parent = MockOpener()
  417.         for url, host, port, type_, dirs, filename, mimetype in [
  418.             ('ftp://localhost/foo/bar/baz.html', 'localhost', ftplib.FTP_PORT, 'I', [
  419.                 'foo',
  420.                 'bar'], 'baz.html', 'text/html'),
  421.             ('ftp://localhost:80/foo/bar/', 'localhost', 80, 'D', [
  422.                 'foo',
  423.                 'bar'], '', None),
  424.             ('ftp://localhost/baz.gif;type=a', 'localhost', ftplib.FTP_PORT, 'A', [], 'baz.gif', None)]:
  425.             r = h.ftp_open(Request(url))
  426.             None(self.assert_ if h.passwd == h.passwd else h.passwd == '')
  427.             self.assertEqual(h.host, socket.gethostbyname(host))
  428.             self.assertEqual(h.port, port)
  429.             self.assertEqual(h.dirs, dirs)
  430.             self.assertEqual(h.ftpwrapper.filename, filename)
  431.             self.assertEqual(h.ftpwrapper.filetype, type_)
  432.             headers = r.info()
  433.             self.assertEqual(headers.get('Content-type'), mimetype)
  434.             self.assertEqual(int(headers['Content-length']), len(data))
  435.         
  436.  
  437.     
  438.     def test_file(self):
  439.         import time as time
  440.         import rfc822 as rfc822
  441.         import socket
  442.         h = urllib2.FileHandler()
  443.         o = h.parent = MockOpener()
  444.         TESTFN = test_support.TESTFN
  445.         urlpath = sanepathname2url(os.path.abspath(TESTFN))
  446.         towrite = 'hello, world\n'
  447.         for url in [
  448.             'file://localhost%s' % urlpath,
  449.             'file://%s' % urlpath,
  450.             'file://%s%s' % (socket.gethostbyname('localhost'), urlpath),
  451.             'file://%s%s' % (socket.gethostbyname(socket.gethostname()), urlpath)]:
  452.             f = open(TESTFN, 'wb')
  453.             
  454.             try:
  455.                 
  456.                 try:
  457.                     f.write(towrite)
  458.                 finally:
  459.                     f.close()
  460.  
  461.                 r = h.file_open(Request(url))
  462.                 
  463.                 try:
  464.                     data = r.read()
  465.                     headers = r.info()
  466.                     newurl = r.geturl()
  467.                 finally:
  468.                     r.close()
  469.  
  470.                 stats = os.stat(TESTFN)
  471.                 modified = rfc822.formatdate(stats.st_mtime)
  472.             finally:
  473.                 os.remove(TESTFN)
  474.  
  475.             self.assertEqual(data, towrite)
  476.             self.assertEqual(headers['Content-type'], 'text/plain')
  477.             self.assertEqual(headers['Content-length'], '13')
  478.             self.assertEqual(headers['Last-modified'], modified)
  479.         
  480.         for url in [
  481.             'file://localhost:80%s' % urlpath]:
  482.             
  483.             try:
  484.                 f = open(TESTFN, 'wb')
  485.                 
  486.                 try:
  487.                     f.write(towrite)
  488.                 finally:
  489.                     f.close()
  490.  
  491.                 self.assertRaises(urllib2.URLError, h.file_open, Request(url))
  492.             finally:
  493.                 os.remove(TESTFN)
  494.  
  495.         
  496.         h = urllib2.FileHandler()
  497.         o = h.parent = MockOpener()
  498.         for url, ftp in [
  499.             ('file://ftp.example.com//foo.txt', True),
  500.             ('file://ftp.example.com///foo.txt', False),
  501.             ('file://ftp.example.com/foo.txt', False)]:
  502.             req = Request(url)
  503.             
  504.             try:
  505.                 h.file_open(req)
  506.             except (urllib2.URLError, OSError):
  507.                 self.assert_(not ftp)
  508.                 continue
  509.  
  510.             self.assert_(o.req is req)
  511.             self.assertEqual(req.type, 'ftp')
  512.         
  513.  
  514.     
  515.     def test_http(self):
  516.         
  517.         class MockHTTPResponse:
  518.             
  519.             def __init__(self, fp, msg, status, reason):
  520.                 self.fp = fp
  521.                 self.msg = msg
  522.                 self.status = status
  523.                 self.reason = reason
  524.  
  525.             
  526.             def read(self):
  527.                 return ''
  528.  
  529.  
  530.         
  531.         class MockHTTPClass:
  532.             
  533.             def __init__(self):
  534.                 self.req_headers = []
  535.                 self.data = None
  536.                 self.raise_on_endheaders = False
  537.  
  538.             
  539.             def __call__(self, host):
  540.                 self.host = host
  541.                 return self
  542.  
  543.             
  544.             def set_debuglevel(self, level):
  545.                 self.level = level
  546.  
  547.             
  548.             def request(self, method, url, body = None, headers = { }):
  549.                 self.method = method
  550.                 self.selector = url
  551.                 self.req_headers += headers.items()
  552.                 if body:
  553.                     self.data = body
  554.                 
  555.                 if self.raise_on_endheaders:
  556.                     import socket
  557.                     raise socket.error()
  558.                 
  559.  
  560.             
  561.             def getresponse(self):
  562.                 return MockHTTPResponse(MockFile(), { }, 200, 'OK')
  563.  
  564.  
  565.         h = urllib2.AbstractHTTPHandler()
  566.         o = h.parent = MockOpener()
  567.         url = 'http://example.com/'
  568.         for method, data in [
  569.             ('GET', None),
  570.             ('POST', 'blah')]:
  571.             req = Request(url, data, {
  572.                 'Foo': 'bar' })
  573.             req.add_unredirected_header('Spam', 'eggs')
  574.             http = MockHTTPClass()
  575.             r = h.do_open(http, req)
  576.             r.read
  577.             r.readline
  578.             r.info
  579.             r.geturl
  580.             (r.code, r.msg == 200, 'OK')
  581.             hdrs = r.info()
  582.             hdrs.get
  583.             hdrs.has_key
  584.             self.assertEqual(r.geturl(), url)
  585.             self.assertEqual(http.host, 'example.com')
  586.             self.assertEqual(http.level, 0)
  587.             self.assertEqual(http.method, method)
  588.             self.assertEqual(http.selector, '/')
  589.             self.assertEqual(http.req_headers, [
  590.                 ('Connection', 'close'),
  591.                 ('Foo', 'bar'),
  592.                 ('Spam', 'eggs')])
  593.             self.assertEqual(http.data, data)
  594.         
  595.         http.raise_on_endheaders = True
  596.         self.assertRaises(urllib2.URLError, h.do_open, http, req)
  597.         o.addheaders = [
  598.             ('Spam', 'eggs')]
  599.         for data in ('', None):
  600.             req = Request('http://example.com/', data)
  601.             r = MockResponse(200, 'OK', { }, '')
  602.             newreq = h.do_request_(req)
  603.             if data is None:
  604.                 self.assert_('Content-length' not in req.unredirected_hdrs)
  605.                 self.assert_('Content-type' not in req.unredirected_hdrs)
  606.             else:
  607.                 self.assertEqual(req.unredirected_hdrs['Content-length'], '0')
  608.                 self.assertEqual(req.unredirected_hdrs['Content-type'], 'application/x-www-form-urlencoded')
  609.             self.assertEqual(req.unredirected_hdrs['Host'], 'example.com')
  610.             self.assertEqual(req.unredirected_hdrs['Spam'], 'eggs')
  611.             req.add_unredirected_header('Content-length', 'foo')
  612.             req.add_unredirected_header('Content-type', 'bar')
  613.             req.add_unredirected_header('Host', 'baz')
  614.             req.add_unredirected_header('Spam', 'foo')
  615.             newreq = h.do_request_(req)
  616.             self.assertEqual(req.unredirected_hdrs['Content-length'], 'foo')
  617.             self.assertEqual(req.unredirected_hdrs['Content-type'], 'bar')
  618.             self.assertEqual(req.unredirected_hdrs['Host'], 'baz')
  619.             self.assertEqual(req.unredirected_hdrs['Spam'], 'foo')
  620.         
  621.  
  622.     
  623.     def test_errors(self):
  624.         h = urllib2.HTTPErrorProcessor()
  625.         o = h.parent = MockOpener()
  626.         url = 'http://example.com/'
  627.         req = Request(url)
  628.         r = MockResponse(200, 'OK', { }, '', url)
  629.         newr = h.http_response(req, r)
  630.         self.assert_(r is newr)
  631.         self.assert_(not hasattr(o, 'proto'))
  632.         r = MockResponse(201, 'Created', { }, '', url)
  633.         self.assert_(h.http_response(req, r) is None)
  634.         self.assertEqual(o.proto, 'http')
  635.         self.assertEqual(o.args, (req, r, 201, 'Created', { }))
  636.  
  637.     
  638.     def test_cookies(self):
  639.         cj = MockCookieJar()
  640.         h = urllib2.HTTPCookieProcessor(cj)
  641.         o = h.parent = MockOpener()
  642.         req = Request('http://example.com/')
  643.         r = MockResponse(200, 'OK', { }, '')
  644.         newreq = h.http_request(req)
  645.         None(self.assert_ if req is req else req is newreq)
  646.         self.assertEquals(req.get_origin_req_host(), 'example.com')
  647.         self.assert_(not req.is_unverifiable())
  648.         newr = h.http_response(req, r)
  649.         self.assert_(cj.ec_req is req)
  650.         None(self.assert_ if r is r else r is newr)
  651.  
  652.     
  653.     def test_redirect(self):
  654.         from_url = 'http://example.com/a.html'
  655.         to_url = 'http://example.com/b.html'
  656.         h = urllib2.HTTPRedirectHandler()
  657.         o = h.parent = MockOpener()
  658.         for code in (301, 302, 303, 307):
  659.             for data in (None, 'blah\nblah\n'):
  660.                 method = getattr(h, 'http_error_%s' % code)
  661.                 req = Request(from_url, data)
  662.                 req.add_header('Nonsense', 'viking=withhold')
  663.                 req.add_unredirected_header('Spam', 'spam')
  664.                 
  665.                 try:
  666.                     method(req, MockFile(), code, 'Blah', MockHeaders({
  667.                         'location': to_url }))
  668.                 except urllib2.HTTPError:
  669.                     if code == 307:
  670.                         pass
  671.                     self.assert_(data is not None)
  672.  
  673.                 self.assertEqual(o.req.get_full_url(), to_url)
  674.                 
  675.                 try:
  676.                     self.assertEqual(o.req.get_method(), 'GET')
  677.                 except AttributeError:
  678.                     self.assert_(not o.req.has_data())
  679.  
  680.                 self.assertEqual(o.req.headers['Nonsense'], 'viking=withhold')
  681.                 self.assert_('Spam' not in o.req.headers)
  682.                 self.assert_('Spam' not in o.req.unredirected_hdrs)
  683.             
  684.         
  685.         req = Request(from_url)
  686.         
  687.         def redirect(h, req, url = to_url):
  688.             h.http_error_302(req, MockFile(), 302, 'Blah', MockHeaders({
  689.                 'location': url }))
  690.  
  691.         req = Request(from_url, origin_req_host = 'example.com')
  692.         count = 0
  693.         
  694.         try:
  695.             while None:
  696.                 count = count + 1
  697.         except urllib2.HTTPError:
  698.             self.assertEqual(count, urllib2.HTTPRedirectHandler.max_repeats)
  699.  
  700.         req = Request(from_url, origin_req_host = 'example.com')
  701.         count = 0
  702.         
  703.         try:
  704.             while None:
  705.                 count = count + 1
  706.         except urllib2.HTTPError:
  707.             self.assertEqual(count, urllib2.HTTPRedirectHandler.max_redirections)
  708.  
  709.  
  710.     
  711.     def test_cookie_redirect(self):
  712.         
  713.         class MockHTTPHandler(urllib2.HTTPHandler):
  714.             
  715.             def __init__(self):
  716.                 self._count = 0
  717.  
  718.             
  719.             def http_open(self, req):
  720.                 import mimetools as mimetools
  721.                 StringIO = StringIO
  722.                 import StringIO
  723.                 if self._count == 0:
  724.                     self._count = self._count + 1
  725.                     msg = mimetools.Message(StringIO('Location: http://www.cracker.com/\r\n\r\n'))
  726.                     return self.parent.error('http', req, MockFile(), 302, 'Found', msg)
  727.                 else:
  728.                     self.req = req
  729.                     msg = mimetools.Message(StringIO('\r\n\r\n'))
  730.                     return MockResponse(200, 'OK', msg, '', req.get_full_url())
  731.  
  732.  
  733.         CookieJar = CookieJar
  734.         import cookielib
  735.         build_opener = build_opener
  736.         HTTPHandler = HTTPHandler
  737.         HTTPError = HTTPError
  738.         HTTPCookieProcessor = HTTPCookieProcessor
  739.         import urllib2
  740.         interact_netscape = interact_netscape
  741.         import test_cookielib
  742.         cj = CookieJar()
  743.         interact_netscape(cj, 'http://www.example.com/', 'spam=eggs')
  744.         hh = MockHTTPHandler()
  745.         cp = HTTPCookieProcessor(cj)
  746.         o = build_opener(hh, cp)
  747.         o.open('http://www.example.com/')
  748.         self.assert_(not hh.req.has_header('Cookie'))
  749.  
  750.  
  751.  
  752. class MiscTests(unittest.TestCase):
  753.     
  754.     def test_build_opener(self):
  755.         
  756.         class MyHTTPHandler(urllib2.HTTPHandler):
  757.             pass
  758.  
  759.         
  760.         class FooHandler(urllib2.BaseHandler):
  761.             
  762.             def foo_open(self):
  763.                 pass
  764.  
  765.  
  766.         
  767.         class BarHandler(urllib2.BaseHandler):
  768.             
  769.             def bar_open(self):
  770.                 pass
  771.  
  772.  
  773.         build_opener = urllib2.build_opener
  774.         o = build_opener(FooHandler, BarHandler)
  775.         self.opener_has_handler(o, FooHandler)
  776.         self.opener_has_handler(o, BarHandler)
  777.         o = build_opener(FooHandler, BarHandler())
  778.         self.opener_has_handler(o, FooHandler)
  779.         self.opener_has_handler(o, BarHandler)
  780.         o = build_opener(MyHTTPHandler)
  781.         self.opener_has_handler(o, MyHTTPHandler)
  782.         o = build_opener()
  783.         self.opener_has_handler(o, urllib2.HTTPHandler)
  784.         o = build_opener(urllib2.HTTPHandler)
  785.         self.opener_has_handler(o, urllib2.HTTPHandler)
  786.         o = build_opener(urllib2.HTTPHandler())
  787.         self.opener_has_handler(o, urllib2.HTTPHandler)
  788.  
  789.     
  790.     def opener_has_handler(self, opener, handler_class):
  791.         for h in opener.handlers:
  792.             if h.__class__ == handler_class:
  793.                 break
  794.                 continue
  795.         
  796.  
  797.  
  798.  
  799. class NetworkTests(unittest.TestCase):
  800.     
  801.     def setUp(self):
  802.         pass
  803.  
  804.     
  805.     def test_range(self):
  806.         req = urllib2.Request('http://www.python.org', headers = {
  807.             'Range': 'bytes=20-39' })
  808.         result = urllib2.urlopen(req)
  809.         data = result.read()
  810.         self.assertEqual(len(data), 20)
  811.  
  812.     
  813.     def test_ftp(self):
  814.         urls = [
  815.             'ftp://www.python.org/pub/python/misc/sousa.au',
  816.             'ftp://www.python.org/pub/tmp/blat',
  817.             'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs']
  818.         self._test_urls(urls, self._extra_handlers())
  819.  
  820.     
  821.     def test_gopher(self):
  822.         urls = [
  823.             'gopher://gopher.lib.ncsu.edu/11/library/stacks/Alex',
  824.             'gopher://gopher.vt.edu:10010/10/33']
  825.         self._test_urls(urls, self._extra_handlers())
  826.  
  827.     
  828.     def test_file(self):
  829.         TESTFN = test_support.TESTFN
  830.         f = open(TESTFN, 'w')
  831.         
  832.         try:
  833.             f.write('hi there\n')
  834.             f.close()
  835.             urls = [
  836.                 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
  837.                 ('file://nonsensename/etc/passwd', None, (OSError, socket.error))]
  838.             self._test_urls(urls, self._extra_handlers())
  839.         finally:
  840.             os.remove(TESTFN)
  841.  
  842.  
  843.     
  844.     def test_http(self):
  845.         urls = [
  846.             'http://www.espn.com/',
  847.             'http://www.python.org/Spanish/Inquistion/',
  848.             ('http://www.python.org/cgi-bin/faqw.py', 'query=pythonistas&querytype=simple&casefold=yes&req=search', None),
  849.             'http://www.python.org/']
  850.         self._test_urls(urls, self._extra_handlers())
  851.  
  852.     
  853.     def _test_urls(self, urls, handlers):
  854.         import socket
  855.         import time
  856.         import logging as logging
  857.         debug = logging.getLogger('test_urllib2').debug
  858.         urllib2.install_opener(urllib2.build_opener(*handlers))
  859.         for url in urls:
  860.             if isinstance(url, tuple):
  861.                 (url, req, expected_err) = url
  862.             else:
  863.                 req = None
  864.                 expected_err = None
  865.             debug(url)
  866.             
  867.             try:
  868.                 f = urllib2.urlopen(url, req)
  869.             except (IOError, socket.error, OSError):
  870.                 err = None
  871.                 debug(err)
  872.                 if expected_err:
  873.                     self.assert_(isinstance(err, expected_err))
  874.                 
  875.             except:
  876.                 expected_err
  877.  
  878.             buf = f.read()
  879.             f.close()
  880.             debug('read %d bytes' % len(buf))
  881.             debug('******** next url coming up...')
  882.             time.sleep(0.10000000000000001)
  883.         
  884.  
  885.     
  886.     def _extra_handlers(self):
  887.         handlers = []
  888.         handlers.append(urllib2.GopherHandler)
  889.         cfh = urllib2.CacheFTPHandler()
  890.         cfh.setTimeout(1)
  891.         handlers.append(cfh)
  892.         return handlers
  893.  
  894.  
  895.  
  896. def test_main(verbose = None):
  897.     tests = (TrivialTests, OpenerDirectorTests, HandlerTests, MiscTests)
  898.     if test_support.is_resource_enabled('network'):
  899.         tests += (NetworkTests,)
  900.     
  901.     test_support.run_unittest(*tests)
  902.  
  903. if __name__ == '__main__':
  904.     test_main(verbose = True)
  905.  
  906.